<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../css/public.css" media="all">
    <style>
        .btnstyle{
            width: 45px;
            height: 30px;
        }
    </style>
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">账号名</label>
                            <div class="layui-input-inline">
                                <input type="text" name="accountName" autocomplete="off" class="layui-input">
                            </div>   
                        </div>
                        
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn btnstyle" lay-event="add" ><img src="../images/ic_add.png" alt="" style="width: 24px;margin-left: -1px;"> </button>
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn btnstyle" lay-event="delete"> <img src="../images/删除 (2).png" alt="" style="width: 24px;margin-left: -1px;"> </button>
             </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>
        
        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit"><img src="../images/修改 (1).png"  style="width: 24px;margin-left: -1px;margin-top: -3px;"></a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete"><img src="../images/删除 (2).png"  style="width: 24px;margin-left: -1px;margin-top: -3px;"></a>
        </script>

        <script type="text/html" id="status-switch">
            <!-- d 表示当前行 -->
            <input type="checkbox" name="status" style="font-size: 10px" disabled="disabled" value="{{=d.accountStatus}}"  lay-skin="switch" lay-filter="demo-templet-status" {{=d.accountStatus == 1 ? "checked" : "" }}>
        </script>

    </div>
</div>
<script src="../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'table'], function () {
        var $ = layui.jquery,
            form = layui.form,
            layer = layui.layer,
            table = layui.table;
        var requrl= "http://localhost:8080/restaurant_web_war_exploded/";
        
        table.render({
            elem:'#currentTableId',
            url: requrl+"account/listAccount",
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                {type: "checkbox", width: 60},
                {field: 'account', width: 80, title: 'ID', sort: true},
                {field: 'phone', width: 120, title: '手机号', sort: true},
                {field:'regisDate', width: 200, title: '注册时间', sort: true},
                {field:'userId', width: 120, title: '用户ID', sort: true},
                {field:'workersId', width: 120, title: '员工ID', sort: true},
                {templet:'#status-switch', width: 365, title: '是否禁用'},
                {title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center"}
            ]],
            parseData:function(res){
                return{
                    code: 0, //解析接口状态
                    msg: res.message, //解析提示文本
                    count: res.data.total, //解析数据长度
                    data: res.data.list //解析数据列表
                }
            },
            limits: [5, 10, 15, 20],
            limit: 5,
            page: true,
            skin: 'line'
        });
        
        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            var result = JSON.stringify(data.field);
            layer.alert(result, {
                title: '最终的搜索信息'
            });

            //执行搜索重载
            table.reload('currentTableId', {
                page: {
                    curr: 1
                }
                , where: {
                    searchParams: result
                }
            }, 'data');

            return false;
        });

        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                var index = layer.open({
                    title: '添加用户',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: 'worker_add.html',
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            } else if (obj.event === 'delete') {  // 监听删除操作
                var checkStatus = table.checkStatus('currentTableId')
                    , data = checkStatus.data;
                layer.alert(JSON.stringify(data));
            }
        });

        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
            console.log(obj)
        });

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                var index = layer.open({
                    title: '编辑用户',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: 'worker_edit.html',
                    success:function(layero, index){
                        var body = layer.getChildFrame('body', index);//获取当前弹窗打开页面Dom元素(对其进行数据回显)
                        var iframeWin = layero.find('iframe')[0].contentWindow;//用于获取当前内容窗口的表单(用于渲染)

                        var body = layer.getChildFrame('body', index);//获取当前弹窗打开页面Dom元素(对其进行数据回显)
                        var iframeWin = layero.find('iframe')[0].contentWindow;//用于获取当前内容窗口的表单(用于渲染)

                        body.find("input[name='wid']").val(obj.data.wid);    
                        body.find("input[name='wname']").val(obj.data.wname);
                        //向下找要回显的元素对其进行回显操作
                        body.find("input[name='code']").val(obj.data.code);
                        body.find("input[name='phone']").val(obj.data.phone);//向下找要回显的元素对其进行回显操作
                        // body.find("input[name='job']").val(obj.data.job);
                        body.find("input[name='entryDate']").val(obj.data.entryDate);
                        //文本框回显
                            
                        body.find("input[name='sex'][value='男']").prop("checked",obj.data.sex=='男'?true:false);
                        body.find("input[name='sex'][value='女']").prop("checked",obj.data.sex=='女'?true:false);
                        body.find("input[name='status'][value='0']").prop("checked",obj.data.status==0?true:false);
                        body.find("input[name='status'][value='1']").prop("checked",obj.data.status==1?true:false);
                        iframeWin.layui.form.render("radio");
                        //复选框回显

                            
                        let myop=data.job;
                        var index=0;
                        let options = body.find("select[name='job'] option");//先拿到option所有值
                        for (let i = 0; i <options.length; i++) {
                            let selop=$(options[i]).val(); 
                            if(selop===myop){
                                index=i;
                                break;  
                            }  
                        }

                        $(options[index]).attr("selected",true);//相中相同项
                        iframeWin.layui.form.render("select");//渲染

                    }   
                });
            } else if (obj.event === 'delete') {
                layer.confirm('真的删除行么', function (index) {
                    obj.del();
                    layer.close(index);
                });
            }
        });

    });
</script>

</body>
</html>